package sangpil.effects
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import gs.TweenLite;
	import gs.easing.Cubic;
	import gs.easing.Quint;
	
	import sangpil.effects.events.SeparateEvent;
	import sangpil.utils.movieClip.Isolation;

	[Event(name='complete', type='sangpil.effects.events.SeparateEvent')]
	public class SeparatePlane extends Sprite
	{
		private var mc:MovieClip;
		private var upMc:Sprite;
		private var downMc:Sprite;
		private var positionY:Number = 0;
		private var remainder:Number = 0;
		public function SeparatePlane(mc:MovieClip)
		{
			super();
			this.mc = mc;
		}
		
		public function start(positionY:Number, remainderHeight:Number):void
		{
			this.positionY = positionY;
			this.remainder = remainderHeight;
			
			if(this.upMc != null)
			{
				this.removeChild(this.upMc);
				this.removeChild(this.downMc);
			}
			
			var planeArr:Array = Isolation.upDown(this.mc, positionY);
			this.upMc =planeArr[0];
			this.downMc =planeArr[1]; 
			this.upMc.y = 0;
			this.downMc.y = positionY;
			this.addChild(this.upMc);
			this.addChild(this.downMc);
			
			//add Tween
			TweenLite.to(this.upMc, 1, {y:this.upMc.height*-1+this.remainder, ease:Cubic.easeInOut, onComplete:onFinishTween, onCompleteParams:[1]});
			TweenLite.to(this.downMc, 0.9, {y:740, ease:Cubic.easeInOut});
			
			this.upMc.addEventListener(MouseEvent.MOUSE_DOWN, goBack);
		}
		
		private function onFinishTween(num:uint):void
		{
			this.dispatchEvent(new SeparateEvent(SeparateEvent.COMPLETE));
		}
		
		public function goBack(e:MouseEvent = null):void
		{
			this.cancle();
		}
		
		public function stop():void
		{

		}
		
		public function cancle():void
		{
			if(this.upMc != null)
			{
				TweenLite.to(this.upMc, 1, {y:0, ease:Quint.easeOut, onComplete:onFinishTween, onCompleteParams:[1]});
				TweenLite.to(this.downMc, 1, {y:this.positionY, ease:Quint.easeOut});
			}
		}
		
	}
}